package com.crealth.handler;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crealth.Config;
import com.crealth.ConstantDefine;
import com.crealth.db.SipConfigDb;
import com.crealth.db.UserDb;
import com.crealth.manager.SipAccountManager;
import com.crealth.memcached.UserCached;
import com.crealth.model.SipConfigModel;
import com.crealth.model.UserModel;
import com.crealth.msg.NetMessageData;
import com.crealth.utils.Utils;

public class SystemSettingHandler {
	//private static Logger errorLog = Logger.getLogger("error_log");
	
	public static NetMessageData obtainInitSipAccount(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() < ConstantDefine.Branch_Mgr_Fu) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		List<SipConfigModel> allSipConfig = SipConfigDb.selectAllSipConfig();		
		resJson.put("sipIP", Config.sipServerIP);
		resJson.put("sipAct", Config.sipAccount);
		resJson.put("sipPwd", Config.sipPwd);
		resJson.put("sipIncomeIP", Config.incomingSipIP);
		resJson.put("sipIncomeAct", Config.incomingSipAccount);
		resJson.put("sipIncomePwd", Config.incomingSipPwd);
		resJson.put("sipDispatchType", Config.sipDispatchType);
		JSONArray jsonArray = new JSONArray();
		for (SipConfigModel sipConfig : allSipConfig) {
			JSONObject sipJson = sipConfig.toJSONObject(null);
			jsonArray.add(sipJson);
		}
		resJson.put("sipConfig", jsonArray);
		resJson.put("resCode", 0);
		sendMsg.setMsgBody(resJson.toJSONString());
		
		return sendMsg;
	}
	
	public static NetMessageData modifyInitSipAccount(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() < ConstantDefine.Branch_Mgr_Fu) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		int sipDispatchType = Utils.getJsonIntValue(msgJson, "sipDispatchType", 0);		
		Config.sipDispatchType = sipDispatchType;
		UserDb.updateSipDispatchType(sipDispatchType);
		String sipIP = msgJson.getString("sipIP");
		String sipAct = msgJson.getString("sipAct");
		String sipPwd = msgJson.getString("sipPwd");
		String sipIncomeIP = msgJson.getString("sipIncomeIP");
		String sipIncomeAct = msgJson.getString("sipIncomeAct");
		String sipIncomePwd = msgJson.getString("sipIncomePwd");
		if (sipDispatchType == 0) {
			Config.sipServerIP = sipIP;
			Config.sipAccount = sipAct;
			Config.sipPwd = sipPwd;
			Config.incomingSipIP = sipIncomeIP;
			Config.incomingSipAccount = sipIncomeAct;
			Config.incomingSipPwd = sipIncomePwd;
			Config.updateSipAccount();
		} else if (sipDispatchType == 2) {
			int companyID = Utils.getJsonIntValue(msgJson, "companyID", 0);
			SipConfigModel sipConfig = new SipConfigModel();
			sipConfig.parseFromJson(msgJson);
			sipConfig.setCompanyID(companyID);
			if (SipAccountManager.companySipConfig.containsKey(companyID)) {				
				SipConfigDb.updateSipConfigByCompanyID(sipConfig);
			} else {
				SipConfigDb.insertSipConfig(sipConfig);
			}
			resJson.put("companyID", companyID);
		}
		
		SipAccountManager.dispatchSipAccount();
		
		resJson.put("sipDispatchType", sipDispatchType);
		resJson.put("sipIP", Config.sipServerIP);
		resJson.put("sipAct", Config.sipAccount);
		resJson.put("sipPwd", Config.sipPwd);
		resJson.put("sipIncomeIP", Config.incomingSipIP);
		resJson.put("sipIncomeAct", Config.incomingSipAccount);
		resJson.put("sipIncomePwd", Config.incomingSipPwd);
		
		resJson.put("resCode", 0);
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	

}
